package com.lingo.mgr.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lingo.common.exception.ServiceException;
import com.lingo.mgr.domain.GptApiKeyConfig;
import com.lingo.mgr.enums.ChatEunm;
import com.lingo.mgr.mapper.GptApiKeyConfigMapper;
import com.lingo.mgr.service.LlmCacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author PC
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LlmCacheServiceImpl implements LlmCacheService {



    private final GptApiKeyConfigMapper gptApiKeyConfigMapper;

    public static Map<Integer,List<String>> apiKeysMap = new ConcurrentHashMap<>();



    @Override
    public List<String> getGptApiKeyConfigs(Integer llmVersion) {
        List<String> apiKeys = apiKeysMap.get(llmVersion);
        if (CollUtil.isEmpty(apiKeys)){
            List<GptApiKeyConfig> gptApiKeyConfigs = gptApiKeyConfigMapper
                .selectList(new LambdaQueryWrapper<GptApiKeyConfig>()
                    .eq(GptApiKeyConfig::getDelFlag, ChatEunm.DEAL_FLAG_0.getStatus())
                    .eq(GptApiKeyConfig::getLlmVersion,llmVersion)
                    .eq(GptApiKeyConfig::getStatus,ChatEunm.APIKEY_STATUS_ONLINE.getStatus()));
            if (CollUtil.isEmpty(gptApiKeyConfigs)){
                gptApiKeyConfigs = gptApiKeyConfigMapper
                    .selectList(new LambdaQueryWrapper<GptApiKeyConfig>()
                        .eq(GptApiKeyConfig::getDelFlag, ChatEunm.DEAL_FLAG_0.getStatus())
                        .eq(GptApiKeyConfig::getLlmVersion,1)
                        .eq(GptApiKeyConfig::getStatus,ChatEunm.APIKEY_STATUS_ONLINE.getStatus()));
            }
            if (CollUtil.isEmpty(gptApiKeyConfigs)) {
                throw new ServiceException("API_KEYS_NOT_NUL");
            }
            apiKeys = gptApiKeyConfigs.stream().
                map(GptApiKeyConfig::getApiKey).collect(Collectors.toList());
            apiKeysMap.put(llmVersion,apiKeys);
        }
        return apiKeys;
    }

    @Override
    @Scheduled(cron = "0 */5 * * * ?")
    @PostConstruct
    public void refreshCache() {
        List<GptApiKeyConfig> gptApiKeyConfigs = gptApiKeyConfigMapper
            .selectList(new LambdaQueryWrapper<GptApiKeyConfig>()
                .eq(GptApiKeyConfig::getDelFlag, ChatEunm.DEAL_FLAG_0.getStatus())
                .eq(GptApiKeyConfig::getStatus,ChatEunm.APIKEY_STATUS_ONLINE.getStatus()));
        if (CollUtil.isNotEmpty(gptApiKeyConfigs)){
            apiKeysMap = gptApiKeyConfigs.stream()
                .collect(Collectors.groupingBy(GptApiKeyConfig::getLlmVersion,
                    Collectors.mapping(GptApiKeyConfig::getApiKey, Collectors.toList())));
        }


    }
}
